Guest User

Untitled

a guest
Aug 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #![feature(concat_idents,duration_as_u128)]
  2.  
  3. extern crate pyo3;
  4.  
  5. use std::thread;
  6. use std::time;
  7.  
  8. use pyo3::prelude::*;
  9. use pyo3::ffi::*;
  10.  
  11. fn make_datetime() -> Py<PyDateTime> {
  12. let gil = Python::acquire_gil();
  13. let py = gil.python();
  14.  
  15. macro_rules! unwrap_py {
  16. ($e:expr) => { ($e).map_err(|e| e.print(py)).unwrap() }
  17. };
  18.  
  19. let datetime = unwrap_py!(py.import("datetime"));
  20. let locals = PyDict::new(py);
  21. locals.set_item("timezone", datetime.get("timezone").unwrap())
  22. .unwrap();
  23. let UTC_ref = unwrap_py!(py.eval("timezone.utc", Some(locals), None));
  24. // .extract::<&'a PyTzInfo>().unwrap().to_object(py);
  25. let UTC = unsafe {
  26. &*(UTC_ref as *const PyObjectRef as *const pyo3::PyObject)
  27. };
  28.  
  29. PyDateTime::new(py, 2011, 1, 1, 0, 0, 0, 0, Some(&UTC)).unwrap()
  30. }
  31.  
  32. fn make_string() -> Py<PyString> {
  33. let gil = Python::acquire_gil();
  34. let py = gil.python();
  35.  
  36. PyString::new(py, "Example")
  37. }
  38.  
  39. fn main() {
  40. let now = time::Instant::now();
  41. let mut handles = Vec::new();
  42. for _thread in 0..2 {
  43. println!("Spawning thread {}", _thread);
  44. let handle = thread::spawn(move || {
  45. for _call in 0..2 {
  46. println!("Starting datetime call {} from thread {}", _thread, _call);
  47. make_datetime();
  48. // make_string();
  49. println!("Finished call {}", _call);
  50. }
  51. });
  52. handles.push(handle);
  53. }
  54.  
  55. for handle in handles.into_iter() {
  56. handle.join().unwrap();
  57. }
  58.  
  59. println!("Done in {}us", now.elapsed().as_micros())
  60.  
  61. }
Add Comment
Please, Sign In to add comment