Skip to content

Parse function keeps crashing, on multi threading  #276

Closed

Description

@Amr-alshroof

i am getting the bellow trace when using parse function on multi threading application, i am not sure if this is happened because parse is not safe on multi threading applications or something else happened:

ERROR:root:*** An Error occured **** Traceback (most recent call last): File "Crawl.py", line 52, in worker self.init_request(url,details) File "Crawl.py", line 141, in init_request self.paginate(r,details) File "Crawl.py", line 135, in paginate self.parse(r,details) File "Crawl.py", line 153, in parse created_date = parse(ad['created'], settings={'PREFER_DATES_FROM': 'past'}) or datetime.datetime.now() File "/usr/local/lib/python2.7/dist-packages/dateparser/conf.py", line 80, in wrapper return f(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/dateparser/__init__.py", line 40, in parse data = parser.get_date_data(date_string, date_formats) File "/usr/local/lib/python2.7/dist-packages/dateparser/date.py", line 360, in get_date_data language, date_string, date_formats, settings=self._settings) File "/usr/local/lib/python2.7/dist-packages/dateparser/date.py", line 164, in parse return instance._parse() File "/usr/local/lib/python2.7/dist-packages/dateparser/date.py", line 174, in _parse date_obj = parser() File "/usr/local/lib/python2.7/dist-packages/dateparser/date.py", line 187, in _try_freshness_parser return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings) File "/usr/local/lib/python2.7/dist-packages/dateparser/freshness_date_parser.py", line 117, in get_date_data date, period = self.parse(date_string, settings) File "/usr/local/lib/python2.7/dist-packages/dateparser/freshness_date_parser.py", line 70, in parse date, period = self._parse_date(date_string) File "/usr/local/lib/python2.7/dist-packages/dateparser/freshness_date_parser.py", line 102, in _parse_date date = self.now - td TypeError: unsupported operand type(s) for -: 'NoneType' and 'relativedelta'

Activity

  1. asadurski commented on Jan 28, 2017

    @asadurski
    Member

    Hello @Amr-alshroof. Yes, it's possible that multithreading is the issue (I will check it further).
    In the meantime, can you give some examples of parsed strings?

  2. Amr-alshroof commented on Jan 29, 2017

    @Amr-alshroof
    Author

    @asadurski here are some examples.
    Today 10:51
    13 Dec

  3. waqasshabbir commented on Feb 24, 2017

    @waqasshabbir
    Contributor

    @Amr-alshroof I tried reproducing the error following way.

    >>> import thread; import dateparser; import time
    
    >>> def print_date(date):
                count = 0
                while count < 5:
                    print repr(dateparser.parse(date))
                    count += 1
                    time.sleep(2)
    
    # starting two threads
    >>> thread.start_new_thread(print_date, ('Friday',)); thread.start_new_thread(print_date, ('Thursday',))
    123145396002816
    datetime.datetime(2017, 2, 17, 0, 0)
    datetime.datetime(2017, 2, 23, 0, 0)
    datetime.datetime(2017, 2, 23, 0, 0)
    datetime.datetime(2017, 2, 17, 0, 0)
    datetime.datetime(2017, 2, 17, 0, 0)
    datetime.datetime(2017, 2, 23, 0, 0)
    datetime.datetime(2017, 2, 23, 0, 0)
    datetime.datetime(2017, 2, 17, 0, 0)
    datetime.datetime(2017, 2, 23, 0, 0)
    datetime.datetime(2017, 2, 17, 0, 0)
    

    On a side note, it's strange to see how self.now is set to NoneType. Maybe you can share your code in a git snippet to enable us to have a deeper look?

    Thanks!

  4. waqasshabbir commented on Mar 13, 2017

    @waqasshabbir
    Contributor

    @Amr-alshroof please re-open if you could reproduce this issue and a script snippet would be very helpful.

  5. 1ucy commented on Mar 30, 2017

    @1ucy

    I'm having the same issue with it breaking on multi threading. It doesn't happen every time but randomly on some calls but not on others. Any chance this could be reopened?

      File "C:\ProgramData\Anaconda2\lib\site-packages\cherrypy\_cprequest.py", line 670, in respond
        response.body = self.handler()
      File "C:\ProgramData\Anaconda2\lib\site-packages\cherrypy\lib\encoding.py", line 220, in __call__
        self.body = self.oldhandler(*args, **kwargs)
      File "C:\ProgramData\Anaconda2\lib\site-packages\cherrypy\lib\jsontools.py", line 61, in json_handler
        value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
      File "C:\ProgramData\Anaconda2\lib\site-packages\cherrypy\_cpdispatch.py", line 60, in __call__
        return self.callable(*self.args, **self.kwargs)
      File "server.py", line 173, in GET
        return self._executeQuery(ds, datefrom, dateto)
      File "server.py", line 125, in _executeQuery
        return self.__execute(dstext, lambda ds: self._query(ds, datefromtext, datetotext))
      File "server.py", line 133, in __execute
        return self._constructReply(get_data_fn(ds))
      File "server.py", line 125, in <lambda>
        return self.__execute(dstext, lambda ds: self._query(ds, datefromtext, datetotext))
      File "server.py", line 109, in _query
        datefrom = dateparser.parse(datefromtext, settings={'TIMEZONE': "UTC"})
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\conf.py", line 80, in wrapper
        return f(*args, **kwargs)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\__init__.py", line 40, in parse
        data = parser.get_date_data(date_string, date_formats)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\date.py", line 360, in get_date_data
        language, date_string, date_formats, settings=self._settings)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\date.py", line 164, in parse
        return instance._parse()
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\date.py", line 174, in _parse
        date_obj = parser()
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\date.py", line 187, in _try_freshness_parser
        return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\freshness_date_parser.py", line 117, in get_date_data
        date, period = self.parse(date_string, settings)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\freshness_date_parser.py", line 70, in parse
        date, period = self._parse_date(date_string)
      File "C:\ProgramData\Anaconda2\lib\site-packages\dateparser\freshness_date_parser.py", line 102, in _parse_date
        date = self.now - td
    TypeError: unsupported operand type(s) for -: 'NoneType' and 'relativedelta'
    
  6. 1ucy commented on Mar 30, 2017

    @1ucy

    Possibly related to issues with self.now = datetime.now(self.get_local_tz()) in parse as this stackoverflow question seems to imply.

  7. asadurski commented on Mar 30, 2017

    @asadurski
    Member

    I gave it another try and I confirm I was able to reproduce that error. It happens when parsing relative dates only. I personally think that's because of the line https://github.com/scrapinghub/dateparser/blob/master/dateparser/freshness_date_parser.py#L110 which resets self.now to None, that might step on another thread's toes. Will dig further soon.

  8. 1ucy commented on Mar 31, 2017

    @1ucy

    Ah, that seems reasonable. For now I've just placed a lock around it but it's a little inelegant.

  9. VMobileRecords commented on Mar 31, 2019

    @VMobileRecords

    Hi @asadurski, I encounter the same error when using QThreadPool and QRunnable in python - my intention is to have 6 threads running in parallel.

    Is there a solution to this, or even a work-around?

    Many thanks in advance!

    File "C:\Program Files (x86)\Python37\lib\site-packages\binance\helpers.py", line 22, in date_to_milliseconds
    d = dateparser.parse(date_str)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\conf.py", line 81, in wrapper
    return f(*args, **kwargs)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser_init_.py", line 53, in parse
    data = parser.get_date_data(date_string, date_formats)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\date.py", line 404, in get_date_data
    locale, date_string, date_formats, settings=self._settings)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\date.py", line 177, in parse
    return instance._parse()
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\date.py", line 187, in _parse
    date_obj = parser()
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\date.py", line 200, in _try_freshness_parser
    return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\freshness_date_parser.py", line 147, in get_date_data
    date, period = self.parse(date_string, settings)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\freshness_date_parser.py", line 96, in parse
    date, period = self._parse_date(date_string)
    File "C:\Program Files (x86)\Python37\lib\site-packages\dateparser\freshness_date_parser.py", line 132, in _parse_date
    date = self.now - td
    TypeError: unsupported operand type(s) for -: 'NoneType' and 'relativedelta'

  10. diazagasatya commented on Aug 15, 2019

    @diazagasatya

    I'm also running a multithread program with this and having the same issue. Try running the thread sequentially not in parallel may fix this issue.

  11. AdrianAtZyte commented on Aug 24, 2026

    @AdrianAtZyte
    Member
  12. serhii73 commented on Aug 25, 2026

    @serhii73
    Collaborator

    Adding the version detail, since several people are subscribed here and the closing pointer to #885 is easy to miss.

    The crash in this report is self.now being instance state on the module-level freshness_date_parser singleton: one thread reset it to None while another was between assigning it and doing self.now - td. #885 (12cfa6f, merged 2021-02-18) removed self.now from dateparser/freshness_date_parser.py altogether and passes the value through as a parameter instead, so that frame can no longer exist.

    The first release containing it is dateparser 1.1.0 (2021-10-04) — it is not in 1.0.0.

    I checked that against the releases rather than only the code. Running the shape of this report — dateparser.parse(s, settings={'PREFER_DATES_FROM': 'past'}) over the strings from it, 16 threads, 3000 calls, CPython 3.11:

    failures
    dateparser 1.0.0 771 / 3000, TypeError: unsupported operand type(s) for +: 'NoneType' and 'relativedelta' at freshness_date_parser.py:138
    dateparser 1.1.0 0 / 3000

    #441's verbatim snippet behaves the same way: 949/2000 failures on 1.0.0, 0/2000 on 1.1.0.

    So if anyone still hits this traceback, it means dateparser < 1.1.0 and upgrading is the fix.

    For completeness: #1346 (merged 2026-08-24, so it ships in the next release, not in 1.4.2) is not what fixed this crash — that was already gone. What it fixes is the thread-safety problems #885 did not touch: KeyError from the size-limited Dictionary caches (#1291), a locale's date order being written onto the shared Settings object mid-parse, and search_dates writing RELATIVE_BASE onto the shared default settings. Those are the reasons concurrent use was still unreliable after 1.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions